package com.gyms.activity;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.classic.okhttp.beans.HVAreaBean;
import com.classic.okhttp.zmoumall.utils.ToastUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gyms.R;
import com.gyms.base.BaseActivity;
import com.gyms.bean.SetLocationBean;
import com.gyms.manager.TitleManager;
import com.gyms.view.TestBottomDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import constant.EventConstant;
import utils.StringUtils;
import weight.UiEvent;

/**
 * 地址设置界面
 */

public class SetAreaActivity extends BaseActivity implements View.OnClickListener {
    private TextView mTvComplete;
    private TextView mTvSetLocation;//设置定位

    private SetLocationBean mSelectedLocationBean;//选择详细地理位置信息
    private EditText mEtPostalCode;//邮政编码
    private EditText mEtAddress;//详细地址
    private EditText mETSetArea;//选择地区

    //选择地区弹出框相关

    private TestBottomDialog mAreasChangeDialog;
    private ArrayList<HVAreaBean> mAreaBeans;
    private static final String S_JSON_FILE_NAME="areas.json";


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_set_area;
    }

    @Override
    protected void initView() {
        setTitle("设置地址");
        mTitleManager.setLeftCloseImg(R.drawable.close_selector);
        setBack();
        initRightView();
        findView();
    }

    private void findView() {
        mTvSetLocation= (TextView) findViewById(R.id.tv_set_location);
        mEtAddress= (EditText) findViewById(R.id.et_address);
        mEtPostalCode= (EditText) findViewById(R.id.et_postal_code);
        mETSetArea= (EditText) findViewById(R.id.et_set_area);
        mETSetArea.setFocusable(false);
        mETSetArea.setFocusableInTouchMode(false);
    }

    private void initRightView() {
        mTvComplete=mTitleManager.setRightText("完成");
        mTvComplete.setTextColor(getResources().getColor(R.color.c_0396EB));
    }

    @Override
    protected void setListener() {
        EventBus.getDefault().register(this);
        mTvSetLocation.setOnClickListener(this);
        mTvComplete.setOnClickListener(this);
        mETSetArea.setOnClickListener(this);
    }

    @Override
    protected void processLogic() {

    }
    @Override
    protected void setTitleTheme() {
        mTheme = TitleManager.TitleTheme.White;
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
            overridePendingTransition(R.anim.activity_close_in, R.anim.activity_close_out);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_right:
                ToastUtil.showToast(mContext,"完成");
                break;
            case R.id.tv_set_location:
                Intent intent = new Intent(mContext,SetLocationActivity.class);
                startActivity(intent, false);
                break;

            case R.id.et_set_area:
                doChangeAreas();
                break;
        }
    }

    @Subscribe
    public void getLocationBean(UiEvent mUiEvent){
        if(mUiEvent.getKey().equals(EventConstant.FLAG_SET_LOCATION)){
             mSelectedLocationBean= (SetLocationBean) mUiEvent.getObject();
            if(null==mSelectedLocationBean){
                return;
            }
            doFullDetailAddress();
        }
    }

    /**
     * 填充细地址详情
     */

    private void doFullDetailAddress() {
        String mStrAddress=mSelectedLocationBean.mPoiItem.getTitle() + "\n" + mSelectedLocationBean.mPoiItem.getSnippet();
        mEtAddress.setText(mStrAddress);
        mEtAddress.setSelection(mStrAddress.length());
        mEtPostalCode.setText(String.valueOf(mSelectedLocationBean.mPoiItem.getPostcode()));
    }

    /**
     * 更改区域
     */

    private void doChangeAreas() {
        if(StringUtils.isNull(mAreaBeans)) {
            initData();
        }

        if(null==mAreasChangeDialog){
            mAreasChangeDialog = TestBottomDialog.newInstance(mAreaBeans);
        }
        mAreasChangeDialog.show(getSupportFragmentManager(),"dialog");
    }
    /**
     * 解析本地json 数据获取列表
     */
    private void initData() {
        String mAreasJson= StringUtils.getJson(mContext,S_JSON_FILE_NAME);
//        mAreaBeans= GsonUtils.listFromJson(mAreasJson);
        mAreaBeans=new Gson().fromJson(mAreasJson,
                new TypeToken<List<HVAreaBean>>() {
                }.getType());
    }
}
